local hx__yuheng = fk.CreateSkill{
  name = "hx__yuheng",
  tags = { Skill.Compulsory },
}


hx__yuheng:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__yuheng.name) and
      table.find(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id) and Fk:getCardById(id).suit ~= Card.NoSuit
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "hx__yuheng_active",
      prompt = "#hx__yuheng-invoke",
      cancelable = false,
    })
    if not (success and dat) then
      dat = {}
      local cards = table.filter(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id) and Fk:getCardById(id).suit ~= Card.NoSuit
      end)
      dat.cards = {cards[1]}
    end
    room:throwCard(dat.cards, hx__yuheng.name, player, player)
    if player.dead then return end
    local all_generals = Fk.generals
    local generals = {}
    for _, general in pairs(all_generals) do
      if general.kingdom == "wu" and general.name ~= player.general and general.name ~= player.deputyGeneral
      and (not general.hidden and not general.total_hidden) and not string.find(general.name, "heg_")
      and not string.find(general.name, "hs_") and not string.find(general.name, "ld_") then
        table.insert(generals, general)
      end
    end
    if #generals == 0 then return end
    local toget_generals = table.random(generals, #dat.cards)
    local skills = {}
    for _, g in ipairs(toget_generals) do
      for _, skill_name in ipairs(g:getSkillNameList(true)) do
        local skill = Fk.skills[skill_name]
        if skill and not skill_name:startsWith("#") and
          not skill_name:endsWith("&") and
          not skill.attached_equip then
          table.insert(skills, skill_name)
        end
      end
    end
    if #skills == 0 then return end
    local mark = player:getTableMark("@&hx__yuheng")
    if mark == nil then
      mark = {}
    end
    for _, g in ipairs(toget_generals) do
      table.insertIfNeed(mark, g.name)
    end
    room:setPlayerMark(player, "@&hx__yuheng", mark)
    room:handleAddLoseSkills(player, table.concat(skills, "|"))
  end,
})

hx__yuheng:addEffect(fk.RoundEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__yuheng.name) and
      table.find(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id) and Fk:getCardById(id).suit ~= Card.NoSuit
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "hx__yuheng_active",
      prompt = "#hx__yuheng-invoke",
      cancelable = false,
    })
    if not (success and dat) then
      dat = {}
      local cards = table.filter(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id) and Fk:getCardById(id).suit ~= Card.NoSuit
      end)
      dat.cards = {cards[1]}
    end
    room:throwCard(dat.cards, hx__yuheng.name, player, player)
    if player.dead then return end
    local all_generals = Fk.generals
    local generals = {}
    for _, general in pairs(all_generals) do
      if general.kingdom == "wu" and general.name ~= player.general and general.name ~= player.deputyGeneral
      and (not general.hidden and not general.total_hidden) and not string.find(general.name, "heg") then
        table.insert(generals, general)
      end
    end
    if #generals == 0 then return end
    local toget_generals = table.random(generals, #dat.cards)
    local skills = {}
    for _, g in ipairs(toget_generals) do
      for _, skill_name in ipairs(g:getSkillNameList(true)) do
        local skill = Fk.skills[skill_name]
        if skill and not skill_name:startsWith("#") and
          not skill_name:endsWith("&") and
          not skill.attached_equip then
          table.insert(skills, skill_name)
        end
      end
    end
    if #skills == 0 then return end
    local mark = player:getTableMark("@&hx__yuheng")
    if mark == nil then
      mark = {}
    end
    for _, g in ipairs(toget_generals) do
      table.insertIfNeed(mark, g.name)
    end
    room:setPlayerMark(player, "@&hx__yuheng", mark)
    room:handleAddLoseSkills(player, table.concat(skills, "|"))
  end,
})


return hx__yuheng
